// 添加左侧表头示例
package com.example.dev_demo.excel.demo;

import com.example.dev_demo.excel.exports.ExcelExportUtil;
import com.example.dev_demo.excel.exports.ExcelHeader;
import com.example.dev_demo.excel.exports.ExcelSheet;
import org.springframework.util.StopWatch;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Excel导出示例
 */
public class ExcelExportDemo {

    public static void main(String[] args) {
        try {
            StopWatch watch = new StopWatch();
            // 创建多个Sheet
            List<ExcelSheet> sheets = new ArrayList<>();

            // 添加第一个Sheet（普通数据，顶部表头）
            watch.start("添加第一个Sheet（顶部表头）");
            ExcelSheet sheet1 = createTopHeaderSheet();
            sheets.add(sheet1);
            watch.stop();

            // 添加第二个Sheet（左侧表头）
            watch.start("添加第二个Sheet（左侧表头）");
            ExcelSheet sheet2 = createLeftHeaderSheet();
            sheets.add(sheet2);
            watch.stop();

            // 添加第三个Sheet（大数据量，左侧表头）
            watch.start("添加第三个Sheet（大数据量左侧表头）");
            ExcelSheet sheet3 = createLargeDataLeftHeaderSheet();
            sheets.add(sheet3);
            watch.stop();

            watch.start("导出Excel");
            // 导出Excel
            try (FileOutputStream fos = new FileOutputStream("Excel导出示例.xlsx")) {
                ExcelExportUtil.exportExcel(sheets, fos);
                watch.stop();
                System.out.println("Excel导出成功！总耗时：" + watch.getTotalTimeMillis() + "ms");
                System.out.println(watch.prettyPrint());
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Excel导出失败：" + e.getMessage());
        }
    }

    /**
     * 创建顶部表头Sheet（传统布局）
     */
    public static ExcelSheet createTopHeaderSheet() {
        int dataCount = 10;
        ExcelSheet sheet = new ExcelSheet("销售报表（顶部表头）," + dataCount / 10000.0 + "万");

        // 添加表头（复杂表头，带合并）
        // 第一行表头
        sheet.addHeader(new ExcelHeader(0, 0, "产品信息", 2, 1));
        sheet.addHeader(new ExcelHeader(2, 0, "销售数据", 3, 1));

        // 第二行表头
        sheet.addHeader(new ExcelHeader(0, 1, "产品ID", 1, 1, "productId"));
        sheet.addHeader(new ExcelHeader(1, 1, "产品名称", 1, 1, "productName"));
        sheet.addHeader(new ExcelHeader(2, 1, "销售数量", 1, 1, "salesVolume"));
        sheet.addHeader(new ExcelHeader(3, 1, "销售金额", 1, 1, "salesAmount"));
        sheet.addHeader(new ExcelHeader(4, 1, "销售日期", 1, 1, "salesDate"));

        // 准备数据
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (int i = 1; i <= dataCount; i++) {
            Map<String, Object> data = new HashMap<>();
            data.put("productId", "P" + i);
            data.put("productName", "产品" + i);
            data.put("salesVolume", i * 10);
            data.put("salesAmount", i * 1000.0);
            data.put("salesDate", "2023-0" + (i % 9 + 1) + "-" + (i % 28 + 1));
            dataList.add(data);
        }

        sheet.setDataList(dataList);
        return sheet;
    }

    /**
     * 创建左侧表头Sheet
     */
    public static ExcelSheet createLeftHeaderSheet() {
        int dataCount = 10000;
        // 创建左侧表头的Sheet
        ExcelSheet sheet = new ExcelSheet("用户信息（左侧表头）" + dataCount / 10000 + "万", ExcelSheet.LayoutDirection.LEFT_HEADER);

        // 添加左侧表头
        sheet.addHeader(new ExcelHeader(0, 0, "用户ID", 1, 1, "userId"));
        sheet.addHeader(new ExcelHeader(0, 1, "用户名", 1, 1, "username"));
        sheet.addHeader(new ExcelHeader(0, 2, "年龄", 1, 1, "age"));
        sheet.addHeader(new ExcelHeader(0, 3, "性别", 1, 1, "gender"));
        sheet.addHeader(new ExcelHeader(0, 4, "邮箱", 1, 1, "email"));

        // 准备数据（每条数据将显示为一列）
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            Map<String, Object> data = new HashMap<>();
            data.put("userId", i);
            data.put("username", "用户" + i);
            data.put("age", 20 + i);
            data.put("gender", i % 2 == 0 ? "女" : "男");
            data.put("email", "user" + i + "@example.com");
            dataList.add(data);
        }

        sheet.setDataList(dataList);
        return sheet;
    }

    /**
     * 创建大数据量左侧表头Sheet
     */
    public static ExcelSheet createLargeDataLeftHeaderSheet() {
        int dataCount = 16000;
        ExcelSheet sheet = new ExcelSheet("大量用户数据（左侧表头）," + (dataCount / 10000.0) + "万", ExcelSheet.LayoutDirection.LEFT_HEADER);
        sheet.setLargeData(true);
        sheet.setRowAccessWindowSize(2000);

        // 添加左侧表头
        sheet.addHeader(new ExcelHeader(0, 0, "横向1", 1, 2));
        sheet.addHeader(new ExcelHeader(0, 2, "横向2", 1, 2));
        sheet.addHeader(new ExcelHeader(1, 0, "ID", 1, 1, "id"));
        sheet.addHeader(new ExcelHeader(1, 1, "名称", 1, 1, "name"));
        sheet.addHeader(new ExcelHeader(1, 2, "地址", 1, 1, "address"));
        sheet.addHeader(new ExcelHeader(1, 3, "电话", 1, 1, "phone"));

        // 准备大数据
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (int i = 1; i <= dataCount; i++) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", i);
            data.put("name", "记录" + i);
            data.put("address", "地址" + i + "号");
            data.put("phone", "13800138" + (i % 1000));
            dataList.add(data);
        }

        sheet.setDataList(dataList);
        return sheet;
    }
}